import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;

public class Client {
	
	public static void main(String[] argv){
		ClientProxy proxy = new ClientProxy();
		
		String file = "/home/mark/Desktop/DistSysStuff/clientfolder/myfile";
		
		BufferedReader userin = new BufferedReader(new InputStreamReader(System.in));
		
//		File f = proxy.getFile(file);
		try{
	
		//	proxy.testEncr();
			/*if(f != null){
	
				BufferedReader brd = new BufferedReader(new FileReader(f));
				BufferedWriter fout = new BufferedWriter(new FileWriter(file));
				
				
				while(brd.ready()){
					fout.write(brd.readLine());
				}
				System.out.println("f.getPath(): " + f.getPath());
				fout.close();
				brd.close();
			*/	
				File f2 = new File("/home/mark/Desktop/DistSysStuff/clientfolder/myfile");
				File floc = new File("/home/mark/Desktop/DistSysStuff/local");
				proxy.getWriteLock(f2);
				
				System.out.println("Have write lock. To update it with ...");
				String update = userin.readLine();
				
				BufferedWriter fout2 = new BufferedWriter(new FileWriter(f2));
				fout2.write(update);
				fout2.flush();
				
				System.out.println("Updated locally. Release lock (and upload)?");
				userin.readLine();
				
				proxy.releaseWriteLock(f2);
				
				/*Local File*/

				proxy.getWriteLock(floc);
				System.out.println("\n\nHave write lock on local file. Updated it with..");
				update = userin.readLine();
				
				BufferedWriter foutlocal = new BufferedWriter(new FileWriter(floc));
				foutlocal.write(update);
				foutlocal.flush();
				
				System.out.println("Updated locally. Release lock (WON'T UPLOAD)?");
				userin.readLine();
				proxy.releaseReadLock(floc);
				
				/*
			}
			else{
				System.out.println("CLIENT: File is null");
			}
		*/
		}catch(Exception e){
			System.out.println("Client error: " + e);
		}
	}
}